<?php


namespace App\Repositories;


use App\Exceptions\BasicException;

class WeekStarRepository
{
    public function getWeekStarGiftList($startAt, $endAt, $id, $gift,$status, $page, $perPage)
    {
        $query1 = app('model_week_star')
            ->where('deleted', 0)
            ->with(['gift_data1:id,name,picture', 'gift_data2:id,name,picture'])
            ->orderBy('period', 'desc')
            ->orderBy('id', 'desc');
        if ($id||$id!==0) {
            $where[] = ['period', '=', $id];
            $query1 = $query1->where($where);
        }
        if ($startAt > 0) {
            $query1 = $query1->whereBetween('weeks', [$startAt, $endAt]);
        }
        if (!empty($gift)){
            $query1 = $query1->Where(function ($query1) use ($gift){
                $query1->orWhere('gift_id_1','=',$gift);
                $query1->orWhere('gift_id_2','=',$gift);
            });
        }
        if ($status > 0){
            $query1 = $query1->where('status',$status);
        }
        $giftList = $query1->paginate($perPage, ['*'], '', $page);
        if ($giftList) return $giftList;
        throw new BasicException(98040);
    }

    public function delWeekStarGift($ids)
    {
        $weekStarGift = [];
        $weekStarGift['deleted'] = 1;
        $newWeekStarGift = app('model_week_star')->whereIn("id", $ids)->update($weekStarGift);
        if ($newWeekStarGift) return true;
        throw new BasicException(98041);
    }

    public function addWeekStarGift($giftId1,$giftId2,$weeks,$period,$room_types)
    {
        $exist = app('model_week_star')->where('weeks',$weeks)->where('deleted',0)->first();
        if ($exist){
            throw new BasicException(10010,$weeks.'周数据已经存在');
        }
        $weekStarGift = [];
        $weekStarGift['gift_id_1'] = $giftId1;
        $weekStarGift['gift_id_2'] = $giftId2;
        $weekStarGift['weeks'] = $weeks;
        $weekStarGift['period'] = $period;
        $weekStarGift['create_time'] = time();
        $weekStarGift['room_types'] = $room_types;
        $newWeekStarGift = app('model_week_star')->create($weekStarGift);
        if ($newWeekStarGift) return true;
        throw new BasicException(98041);
    }
    public function upWeekStarGift($id,$giftId1,$giftId2,$weeks,$period,$room_types)
    {
        $where[]=['id','=',$id];
        $weekStarGift = [];
        if ($giftId1 > 0) {
            $weekStarGift['gift_id_1'] = $giftId1;
        }
        if ($giftId2 > 0) {
            $weekStarGift['gift_id_2'] = $giftId2;
        }

        $weekStarGift['weeks'] = $weeks;
        $weekStarGift['period'] = $period;
        $weekStarGift['update_time'] = time();
        $weekStarGift['room_types'] = $room_types;
        $newWeekStarGift = app('model_week_star')->where($where)->update($weekStarGift);
        if ($newWeekStarGift) return true;
        throw new BasicException(98041);
    }

    /**
     * 获取最近一周的数字
     * @return int
     */
    public function getLastWeek()
    {
        $last = app('model_week_star')->where('deleted',0)->orderBy('period','desc')->first();
        if ($last){
            return $last->period;
        }
        return 0;
    }
}
